Tags: #◇Computer Science #◇CSARCH2 #◇Computer Architecture #◇Floating-Point Rounding #◇Rounding Modes #◇Guard Round Sticky #◇Truncation #◇IEEE-754
Problem Set: Floating-Point Rounding
◇Overview & Theoretical Foundations
During floating-point arithmetic (e.g., alignment shifts, multiplication, division), intermediate calculations frequently generate more bits or digits of precision than can fit into the destination register (e.g., 24 bits in single precision, 53 bits in double precision). Rounding is the process of mapping an exact infinite-precision or extended-precision real number to the nearest representable floating-point number .
IEEE-754 Rounding Direction on the Real Number Line
<--- Round Down (-oo) ────────────────────────────────── Round Up (+oo) --->
───────────────────┬───────────────────────┼───────────────────────┬───────>
x_lower x x_upper
│ │ │
└─────── Truncate ──────┴─────── Truncate ──────┘
(if x > 0) (if x < 0)
--> 0 <--
1. The Four IEEE-754 Rounding Modes
| Rounding Mode | Symbol / Alias | Mathematical Definition for | Behavior on Negative Numbers () | Behavior on Positive Numbers () |
|---|---|---|---|---|
| Truncate | Round toward (Chop) | Discards trailing digits (moves rightward toward ) | Discards trailing digits (moves leftward toward ) | |
| Round Up | Round toward (Ceiling) | Discards trailing digits (less negative, moves right toward ) | Adds to LSB if discarded digits (moves right away from ) | |
| Round Down | Round toward (Floor) | Adds to magnitude if discarded digits (more negative, moves left) | Discards trailing digits (moves left toward ) | |
| Round to Nearest (Ties to Even) | Banker's Rounding (Default) | Rounds to closest value; on exact halfway tie, chooses LSB (even) | Rounds to closest value; on exact halfway tie, chooses LSB (even) |
2. Guard (), Round (), and Sticky () Bits in Binary Hardware
To perform IEEE-754 compliant rounding without maintaining an infinite number of hardware flip-flops, floating-point units (FPUs) append three auxiliary bits during intermediate alignment and arithmetic:
Significand Register (p bits) Auxiliary Bits
┌───┬───┬───┬───┬───┬───┬───┬───┐ ┌───┬───┬───┐
│b0 │b1 │b2 │...│...│...│...│bp │ │ G │ R │ S │
└───┴───┴───┴───┴───┴───┴───┴───┘ └───┴───┴───┘
LSB │ │ └── Logical OR of all bits
│ │ shifted beyond R (2^-(p+2)...)
│ └────── Second discarded bit (2^-(p+1))
└────────── First discarded bit (2^-p)
Round to Nearest (Ties to Even) Decision Rule:
- Case 1:
- Discarded value is of the LSB unit.
- Action: Round DOWN (Truncate / Do not add to LSB).
- Case 2: and ( or )
- Discarded value is of the LSB unit.
- Action: Round UP (Add to LSB of significand).
- Case 3: and and (Exact Halfway Tie)
- Discarded value is exactly equal to LSB.
- Action (Ties to Even):
- If current LSB is (Even) Keep as is (Do not add ).
- If current LSB is (Odd) Add to LSB to make it Even ().
◇Problem Solutions
Problem 1: Rounding the Decimal Value
Problem Statement
Apply all four floating-point rounding modes to the decimal number for:
- a.) 4 decimal digits
- b.) 3 decimal digits
Step-by-Step Derivation
Part (a): Rounding to 4 Decimal Digits (4 Fractional Places)
- Input:
- Target Precision: 4 digits after the decimal point (retaining ).
- Retained Prefix:
- Discarded Part: (the decimal digit is , with no further nonzero digits).
- Bracketing Representable Numbers:
-
Truncate (Round toward ):
- Discard the trailing .
- Moving toward increases the value of a negative number:
-
Round Up (Round toward ):
- The ceiling at 4 decimal places is (since ).
- The ceiling at 4 decimal places is (since ).
-
Round Down (Round toward ):
- The floor at 4 decimal places is (since ).
- The floor at 4 decimal places is (since ).
-
Round to Nearest (Ties to Even):
- Distance to : .
- Distance to : .
- Exact Halfway Tie: Compare least significant retained digit:
- In , the last digit is (Odd).
- In , the last digit is (Even).
- Tie breaks toward the even digit:
- Tie breaks toward the even digit:
Part (b): Rounding to 3 Decimal Digits (3 Fractional Places)
- Input:
- Target Precision: 3 digits after the decimal point (retaining ).
- Retained Prefix:
- Discarded Part: (digits ).
- Bracketing Representable Numbers:
-
Truncate (Round toward ):
- Discard the .
- Discard the .
-
Round Up (Round toward ):
- Moving toward picks the algebraically larger number:
- Moving toward picks the algebraically larger number:
-
Round Down (Round toward ):
- Moving toward picks the algebraically smaller number:
- Moving toward picks the algebraically smaller number:
-
Round to Nearest (Ties to Even):
- The discarded portion is .
- Comparing to the half-step :
- Since the discarded value is strictly greater than halfway, is strictly closer to (distance ) than to (distance ).
- No tie-breaking is invoked; standard nearest applies:
Result Summary Table (Problem 1)
| Precision Target | Truncate (toward 0) | Round Up (toward ) | Round Down (toward ) | Round to Nearest (Ties to Even) |
|---|---|---|---|---|
| a.) 4 Decimal Digits (places) | -1.8685 | -1.8685 | -1.8686 | -1.8686 (tie even) |
| b.) 3 Decimal Digits (places) | -1.868 | -1.868 | -1.869 | -1.869 ( nearest) |
[!note] Alternative Context: Total Significant Digits
If interpreted as total significant digits (1 integer + fractional digits):
- 4 Total Digits (1 int + 3 frac): Truncate:
-1.868, Round Up:-1.868, Round Down:-1.869, Round Nearest:-1.869.- 3 Total Digits (1 int + 2 frac): Discarded is Truncate:
-1.86, Round Up:-1.86, Round Down:-1.87, Round Nearest:-1.87.
Problem 2: Rounding the Binary Value
Problem Statement
Apply all four floating-point rounding modes to the binary floating-point significand for:
- a.) 4 binary digits
- b.) 3 binary digits
Step-by-Step Derivation
Part (a): Rounding to 4 Binary Digits (4 Fractional Bits)
- Input:
- Target Precision: 4 bits after the radix point (retaining ).
- Retained Prefix:
- Discarded Bits: ().
- Observation: The discarded bits are identically zero. The value is already exact at 4 fractional binary digits.
-
Truncate (toward ):
-
Round Up (toward ):
-
Round Down (toward ):
-
Round to Nearest (Ties to Even):
(All 4 modes produce identical results because no truncation error exists).
Part (b): Rounding to 3 Binary Digits (3 Fractional Bits)
- Input:
- Target Precision: 3 bits after the radix point (retaining ).
- Retained Prefix:
- Discarded Bits:
- Guard Bit (, bit 4):
- Round Bit (, bit 5):
- Sticky Bit (, remaining):
- Bracketing Representable Binary Numbers:
- Actual Exact Value: .
-
Truncate (Round toward ):
- Discard the bits to move toward :
- Discard the bits to move toward :
-
Round Up (Round toward ):
- Moving toward picks the algebraically larger number:
- Moving toward picks the algebraically larger number:
-
Round Down (Round toward ):
- Moving toward subtracts LSB () to make it more negative:
- Moving toward subtracts LSB () to make it more negative:
-
Round to Nearest (Ties to Even):
- Discarded portion is .
- Comparing against the halfway threshold of (which is ):
- Because the discarded bits exceed the halfway mark (), is strictly closer to (distance ) than to (distance ).
- Therefore, it rounds to the nearest value:
Result Summary Table (Problem 2)
| Precision Target | Truncate (toward 0) | Round Up (toward ) | Round Down (toward ) | Round to Nearest (Ties to Even) |
|---|---|---|---|---|
| a.) 4 Binary Digits (places) | -1.0111 | -1.0111 | -1.0111 | -1.0111 (Exact) |
| b.) 3 Binary Digits (places) | -1.011 | -1.011 | -1.100 | -1.100 () |
[!note] Alternative Context: Total Significant Bits
If interpreted as total significant bits (1 integer bit + fractional bits):
- 4 Total Bits (1 int + 3 frac): Yields identical results to row (b) above (
-1.011,-1.011,-1.100,-1.100).- 3 Total Bits (1 int + 2 frac): Kept: , Discarded: Truncate:
-1.01, Round Up:-1.01, Round Down:-1.10, Round Nearest:-1.10.
◇Python Verification Script
from decimal import Decimal, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_EVEN
def verify_decimal_rounding():
val = Decimal('-1.86855')
# 4 decimal places
q4 = Decimal('0.0001')
t4 = val.quantize(q4, rounding=ROUND_DOWN)
u4 = val.quantize(q4, rounding=ROUND_CEILING)
d4 = val.quantize(q4, rounding=ROUND_FLOOR)
n4 = val.quantize(q4, rounding=ROUND_HALF_EVEN)
print(f"Decimal 4 places -> Trunc: {t4}, Up: {u4}, Down: {d4}, Nearest: {n4}")
# 3 decimal places
q3 = Decimal('0.001')
t3 = val.quantize(q3, rounding=ROUND_DOWN)
u3 = val.quantize(q3, rounding=ROUND_CEILING)
d3 = val.quantize(q3, rounding=ROUND_FLOOR)
n3 = val.quantize(q3, rounding=ROUND_HALF_EVEN)
print(f"Decimal 3 places -> Trunc: {t3}, Up: {u3}, Down: {d3}, Nearest: {n3}")
verify_decimal_rounding()
◇Key Takeaways & Rounding Decision Table
- Negative Value Directions:
- On the negative side of the real line, moving toward (Round Up) decreases the magnitude (less negative, identical to truncation toward zero).
- Moving toward (Round Down) increases the magnitude (more negative, subtracts LSB).
- Ties to Even (Banker's Rule):
- Active only when the discarded fraction is exactly halfway ( in binary; followed by all s in decimal).
- If discarded portion , always round to the larger magnitude (UP).
- If discarded portion , always round to the smaller magnitude (DOWN).
- Hardware Efficiency of Sticky Bit:
- A single bit replaces hundreds of comparison bits by accumulating during right-shift alignment.
◇References & Navigation
- Related Notes: ◇Floating-Point Operations & Errors